package com.jinwei.qunguanjia.web.group.controller;

import com.jinwei.qunguanjia.common.core.domain.Response;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.utils.PageDataUtil;
import com.jinwei.qunguanjia.common.utils.PageUtils;
import com.jinwei.qunguanjia.user.dto.SensitiveWordDTO;
import com.jinwei.qunguanjia.user.service.SensitiveWordService;
import com.jinwei.qunguanjia.web.group.vo.SensitiveWordVO;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 敏感词管理相关接口
 *
 * @author yuanzs
 */
@RestController
@RequestMapping("/sensitiveWord")
public class SensitiveWordController {

    @Resource
    private SensitiveWordService sensitiveWordService;

    /**
     * 敏感词添加接口
     * @param sensitiveWordVO 对象
     */
    @PostMapping("/save")
    public Response save(@Validated @RequestBody SensitiveWordVO sensitiveWordVO) {
        SensitiveWordDTO sensitiveWordDTO = new SensitiveWordDTO();
        BeanUtils.copyProperties(sensitiveWordVO, sensitiveWordDTO);
        sensitiveWordService.save(sensitiveWordDTO);
        return Response.success();
    }

    /**
     * 敏感词修改接口
     * @param sensitiveWordVO 对象
     */
    @PutMapping("/update")
    public Response update(@Validated @RequestBody SensitiveWordVO sensitiveWordVO) {
        SensitiveWordDTO sensitiveWordDTO = new SensitiveWordDTO();
        BeanUtils.copyProperties(sensitiveWordVO, sensitiveWordDTO);
        sensitiveWordService.update(sensitiveWordDTO);
        return Response.success();
    }

    /**
     * 获取敏感词分页列表
     * @param sensitiveWordVO 对象
     */
    @GetMapping("/listPage")
    public PageDataInfo<SensitiveWordVO> list(SensitiveWordVO sensitiveWordVO) throws IllegalAccessException {
        SensitiveWordDTO sensitiveWordDTO = new SensitiveWordDTO();
        BeanUtils.copyProperties(sensitiveWordVO, sensitiveWordDTO);
        if (null != sensitiveWordVO.getSensitiveWordContent()) {
            sensitiveWordDTO.setSensitiveWordContent("like:"+sensitiveWordVO.getSensitiveWordContent());
        }
        PageUtils.startPage();
        PageDataUtil<SensitiveWordDTO, SensitiveWordVO> pageDataUtil = new PageDataUtil<>();
        return pageDataUtil.transform(sensitiveWordService.listPage(sensitiveWordDTO), SensitiveWordVO.class);
    }
}
